/**
 * @Author
 * @Date 2020-11-25 11:34:46
 * @Desc 通话面板和对话面板通信桥
 */
const XMESSAGE_CONTEXT = '/xmessage-cms' // 融合通信上下文

export default class Communication {
  constructor(props) {
    const {
      callPanelSrc,
      chatPanelSrc,
      inviteDialogSrc,
      dev,
      devServe,
      audioWidth,
      audioHeight,
      videoWidth,
      videoHeight,
      intercomWidth,
      intercomHeight,
    } = props || {} // 自定义iframe地址
    this.instance = null
    this.devServe = dev ? devServe : '' // 开发环境的ip

    this.initCallPanel(callPanelSrc)
    this.initChatPanel(chatPanelSrc)
    this.initInviteDialog(inviteDialogSrc)
    this.iframeSizeOpt = {
      audioWidth,
      audioHeight,
      videoWidth,
      videoHeight,
      intercomWidth,
      intercomHeight,
    }
    this.initMessageListener({
      ...this.iframeSizeOpt,
    })
    this.postMessageFun = null // 监听消息函数
  }

  /**
   * @author
   * @date 2020-11-27 10:19:12
   * @desc 初始化对话框面板
   */
  initChatPanel(src) {
    const { devServe } = this
    this.createIframe({
      src: src || `${devServe}${XMESSAGE_CONTEXT}/iframe/chatPanel`,
      iframeId: 'chatPanelIframe',
      zIndex: 997,
      width: '968px',
      height: '710px',
    })
  }

  /**
   * @author
   * @date 2020-11-27 10:19:04
   * @desc 初始化通话面板
   */
  initCallPanel(src) {
    const { devServe } = this
    this.createIframe({
      src: src || `${devServe}${XMESSAGE_CONTEXT}/iframe/callPanel`,
      iframeId: 'callPanelIframe',
      zIndex: 998,
      width: `340px`,
      height: `600px`,
    })
  }

  /**
   * @author
   * @date 2020-11-27 10:19:12
   * @desc 初始化邀请成员弹窗
   */
  initInviteDialog(src) {
    const { devServe } = this
    this.createIframe({
      src: src || `${devServe}${XMESSAGE_CONTEXT}/iframe/inviteDialog`,
      iframeId: 'inviteDialogIframe',
      zIndex: 999,
      width: '576px',
      height: '455px',
    })
  }

  /**
   * @author
   * @date 2020-12-10 10:00:55
   * @desc 创建iframe
   */
  createIframe(pos) {
    const { src, iframeId, zIndex, width, height } = pos
    const iframeEl = document.createElement('iframe')
    iframeEl.src = src
    iframeEl.id = iframeId
    iframeEl.allow = 'speaker;microphone;camera;'
    iframeEl.style.position = 'fixed'
    iframeEl.style['z-index'] = zIndex
    iframeEl.style.top = 0
    iframeEl.style.left = 0
    iframeEl.style.bottom = 0
    iframeEl.style.right = 0
    iframeEl.style.width = width
    iframeEl.style.height = height
    iframeEl.style.margin = 'auto'
    iframeEl.style.display = 'none'
    document.body.appendChild(iframeEl)
  }

  /**
   * @author
   * @date 2020-11-27 11:12:02
   * @desc 接收子页面的推送消息
   */
  initMessageListener(opt) {
    window.addEventListener('message', e => {
      const { type } = e.data
      const iframe = document.querySelector('#callPanelIframe')
      this.setIframeSize(iframe, this.iframeSizeOpt, e.data)
      switch (type) {
        // 呼叫操作
        case 'call': {
          // const iframe = document.querySelector('#callPanelIframe')
          this.doCall({ ...e.data, ...opt })
          break
        }

        // 显示通话面板iframe
        case 'showCallPanel': {
          this.displayCallPanel(true)
          break
        }

        // 隐藏通话面板iframe
        case 'hideCallPanel': {
          this.displayCallPanel(false)
          break
        }

        // 隐藏对话面板
        case 'hideChatPanel': {
          this.displayChatPanel(false)
          break
        }

        // 显示邀请弹窗
        case 'showInviteDialog': {
          this.displayInviteDialog(true, e.data)
          break
        }

        // 点击邀请弹窗的确认按钮,发送数据给通话面板
        case 'sendInviteDialogDataToCall': {
          document.querySelector('#callPanelIframe').contentWindow.postMessage(e.data, '*')
          this.displayInviteDialog(false)
          break
        }

        // 隐藏邀请弹窗
        case 'hideInviteDialog': {
          this.displayInviteDialog(false)
          break
        }

        // 文本消息推送通知
        case 'postMsg': {
          this.postMessageFun && this.postMessageFun(e.data)
          break
        }

        default:
          break
      }
    })
  }

  /**
   * @author
   * @date 2020-11-27 10:18:54
   * @desc 显示对话面板
   */
  displayChatPanel(show) {
    document.querySelector('#chatPanelIframe').style.display = show ? 'block' : 'none'
  }

  /**
   * @author
   * @date 2020-11-30 15:13:21
   * @desc 指定到某一聊天对话
   */
  assignToChat(data) {
    this.displayChatPanel(true)
    document.querySelector('#chatPanelIframe').contentWindow.postMessage({ type: 'assignChat', ...data }, '*')
  }

  /**
   * @author
   * @date 2020-11-27 11:36:37
   * @desc 显示通话面板iframe
   */
  displayCallPanel(show, data) {
    document.querySelector('#callPanelIframe').style.display = show ? 'block' : 'none'
  }

  /**
   * @author
   * @date 2020-11-27 10:55:23
   * @desc 拨打电话
   */
  doCall(data) {
    const iframe = document.querySelector('#callPanelIframe')
    this.setIframeSize(iframe, this.iframeSizeOpt, data)
    this.displayCallPanel(true, data)
    document.querySelector('#callPanelIframe').contentWindow.postMessage({ type: 'call', ...data }, '*')
  }

  /**
   * @author taoyuqing
   * @date 2023-03-15 17:16:17
   * @desc 设置窗口大小
   */
  setIframeSize(iframe, iframeSizeOpt, callData) {
    const { audioWidth, audioHeight, videoWidth, videoHeight, intercomWidth, intercomHeight } = iframeSizeOpt
    if (iframe) {
      iframe.style.width = `${audioWidth || 340}px`
      iframe.style.height = `${audioHeight || 600}px`
    }
    if (iframe && callData.useVideo) {
      iframe.style.width = `${videoWidth || 340}px`
      iframe.style.height = `${videoHeight || 600}px`
    }
    if (iframe && callData.intercomNumber) {
      iframe.style.width = `${intercomWidth || 340}px`
      iframe.style.height = `${intercomHeight || 600}px`
    }
  }

  /**
   * @author
   * @date 2020-11-27 17:01:13
   * @desc 展示邀请弹窗
   */
  displayInviteDialog(show, data) {
    document.querySelector('#inviteDialogIframe').style.display = show ? 'block' : 'none'
    show && document.querySelector('#inviteDialogIframe').contentWindow.postMessage(data, '*')
  }

  /**
   * @author
   * @date 2021-03-16 09:42:50
   * @desc 监听新消息推送
   */
  listenNewMsg(cb) {
    this.postMessageFun = cb
  }

  /**
   * @author
   * @date 2020-11-27 10:29:46
   * @desc 实例化
   */
  static getInstance(options) {
    // 未创建实例
    if (!this.instance) {
      const {
        callPanelSrc,
        chatPanelSrc,
        inviteDialogSrc,
        dev,
        devServe,
        audioWidth,
        audioHeight,
        videoWidth,
        videoHeight,
        intercomWidth,
        intercomHeight,
      } = options || {}
      this.instance = new Communication({
        callPanelSrc,
        chatPanelSrc,
        inviteDialogSrc,
        dev,
        devServe,
        audioWidth,
        audioHeight,
        videoWidth,
        videoHeight,
        intercomWidth,
        intercomHeight,
      })
      return this.instance
    }
    return this.instance
  }
}
